﻿using System.Collections.Generic;
using AiFrame.InterfaceLib.Plugins;

namespace AiFrame.Base.Core.Plugins
{
    class FactoryManager: IFactoryManager
    {
        private static FactoryManager _instance;
        private List<IFactory> _factoryList;

        public static FactoryManager Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new FactoryManager();

                return _instance;
            }
        }
        
        private FactoryManager()
        {
            _factoryList = new List<IFactory>();
        }

        #region Implementation of IFactoryManager

        public void RegisterFactory(IFactory factory)
        {
            if (_factoryList.IndexOf(factory) == -1)
                _factoryList.Add(factory);
        }

        public void UnregisterFactory(IFactory factory)
        {
            _factoryList.Remove(factory);
        }

        public T GetFactory<T>()
        {
            foreach (IFactory factory in _factoryList)
            {
                if (factory is T)
                    return (T) factory;
            }
            return default(T);
        }

        #endregion
    }
}
